package edu.ynu.se.xiecheng.achitectureclass.controller;

import edu.ynu.se.xiecheng.achitectureclass.dto.CameraDTO;
import edu.ynu.se.xiecheng.achitectureclass.service.CameraService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/cameras")
@Api(tags = "摄像头控制接口")
public class CameraController {

    @Autowired
    private CameraService cameraService;

    @PostMapping("/capture")
    @ApiOperation("抓拍摄像头当前画面")
    public ResponseEntity<String> captureImage(
            @RequestParam String deviceSerial,
            @RequestParam(defaultValue = "1") Integer channelNo,
            @RequestParam(required = false) Integer quality) {

        String picUrl = cameraService.captureImage(deviceSerial, channelNo, quality);
        if (picUrl != null) {
            return ResponseEntity.ok(picUrl);
        } else {
            return ResponseEntity.badRequest().body("Failed to capture image");
        }
    }

    @GetMapping("/{deviceSerial}/captures")
    @ApiOperation("获取指定摄像头的抓拍历史")
    public ResponseEntity<List<CameraDTO>> getCameraCaptures(@PathVariable String deviceSerial) {
        List<CameraDTO> captures = cameraService.getLatestCaptures(deviceSerial);
        return ResponseEntity.ok(captures);
    }
}